<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="formGroup">
  <mat-toolbar fxLayout="row" color="primary">
    <h2 translate>{{ title }}</h2>
    <span fxFlex></span>
    <button mat-icon-button
            [mat-dialog-close]="null"
            type="button"
            matTooltip="{{ 'action.close' | translate }}"
            matTooltipPosition="above">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
  </mat-progress-bar>
  <div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div>
  <div mat-dialog-content>
    <section fxFlex fxLayout="row" fxLayoutGap="16px" fxLayoutAlign="flex-start center">
      <mat-form-field fxFlex class="mat-block">
        <mat-label translate>ws-client.subscriptions.topic-filter</mat-label>
        <input matInput required formControlName="topicFilter" placeholder="{{ 'ws-client.subscriptions.topic-filter' | translate }}">
        <tb-copy-button matSuffix
                        [fxShow]="formGroup.get('topicFilter')?.value?.length"
                        [copyText]="formGroup.get('topicFilter')?.value">
        </tb-copy-button>
        <mat-error *ngIf="formGroup.get('topicFilter').hasError('required')">
          {{ 'ws-client.subscriptions.topic-filter-required' | translate }}
        </mat-error>
      </mat-form-field>
      <tb-color-input formControlName="color" asBoxInput colorClearButton style="padding-bottom: 20px">
      </tb-color-input>
    </section>
    <mat-form-field fxFlex class="mat-block" hideRequiredMarker>
      <mat-label translate>mqtt-client-session.qos</mat-label>
      <mat-select formControlName="qos" placeholder="{{ 'mqtt-client-session.qos' | translate }}">
        <mat-option *ngFor="let qos of qoSTypes" [value]="qos">
          {{ qoSTranslationMap.get(qos) | translate }}
        </mat-option>
      </mat-select>
    </mat-form-field>
    <section formGroupName="options" fxLayout="column" fxLayoutGap="16px">
      <mat-accordion multi="true" class="mat-body-1">
        <mat-expansion-panel>
          <mat-expansion-panel-header>
            <mat-panel-title>
              {{ 'ws-client.subscriptions.mqtt-5-features' | translate }}
            </mat-panel-title>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            <div fxLayout="column" fxLayoutGap="16px">
              <mat-form-field fxFlex class="mat-block" subscriptSizing="dynamic">
                <mat-label translate>ws-client.subscriptions.rh</mat-label>
                <mat-select formControlName="retainHandling">
                  <mat-option *ngFor="let rh of rhOptions" [value]="rh.value">
                    {{ rh.name }}
                  </mat-option>
                </mat-select>
              </mat-form-field>
              <section class="slider" fxLayout="row" fxLayoutAlign="flex-start center" fxLayoutGap="8px">
                <mat-slide-toggle formControlName="noLocal">
                  {{ 'ws-client.subscriptions.nl' | translate }}
                </mat-slide-toggle>
                <mat-icon class="tb-mat-20" style="color: rgba(0,0,0,0.18)"
                          [matTooltip]="'ws-client.subscriptions.nl-hint' | translate"
                          matTooltipPosition="right">
                  info</mat-icon>
              </section>
              <section class="slider" fxLayout="row" fxLayoutAlign="flex-start center" fxLayoutGap="8px">
                <mat-slide-toggle formControlName="retainAsPublish">
                  {{ 'ws-client.subscriptions.rap' | translate }}
                </mat-slide-toggle>
                <mat-icon class="tb-mat-20" style="color: rgba(0,0,0,0.18)"
                          [matTooltip]="'ws-client.subscriptions.rap-hint' | translate"
                          matTooltipPosition="right">
                  info</mat-icon>
              </section>
            </div>
          </ng-template>
        </mat-expansion-panel>
      </mat-accordion>
    </section>
  </div>
  <div mat-dialog-actions fxLayout="row" fxLayoutAlign="end center">
    <button mat-button color="primary"
            type="button"
            [disabled]="(isLoading$ | async)"
            [mat-dialog-close]="null" cdkFocusInitial>
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            (click)="save()"
            [disabled]="(isLoading$ | async) || formGroup?.invalid">
      {{ actionButtonLabel | translate }}
    </button>
  </div>
</form>
